<?php

	function vnnews_display_newslider($cat_id, $number_post, $slider_speed, $slider_effect, $slider_direction)
	{
		$args = array(
					'cat' => $cat_id,
					'post_status' => 'publish',
					'ignore_sticky_posts' => 1,
					'posts_per_page' => $number_post
				);

		$str = '<section class="slider"><div class="flexslider loading" data-speed="'. $slider_speed .'" data-transition="'. $slider_effect .'" data-direction="'. $slider_direction .'"><ul class="slides">';

		$query = new WP_Query( $args );

		if ( $query -> have_posts() ) :

			while ( $query -> have_posts() ) : $query -> the_post();

				$post_id = get_the_ID();
				$link = get_permalink();
				$title = the_title('','',FALSE);
				$short_title = mb_substr($title, 0, 80);
				if (strlen($title) > 80){
					$short_title .= ' ...';
				}
				$excerpt = get_the_excerpt();
				$excerpt = (strlen($excerpt) > 140) ? mb_substr($excerpt, 0, 140). ' ...' : $excerpt;

				$categories = get_the_category($post_id );
				$cat_name = $categories[0]->cat_name;
				$cat_link = get_category_link($categories[0]->term_id);

				$main_thumb = get_the_post_thumbnail( $post_id, array(360,202 ));
				$thumb_id = get_post_thumbnail_id( $post_id );
				//$data_thumb = wp_get_attachment_image_src($thumb_id, array(75, 75 ))[0];

				//data-thumb="'.$data_thumb.'" data-thumbcaption="'.$short_title.'" (properties in li tag)
	            $str .= '<li class="clearfix"><div class="row">';
  	    	    $str .= '<div class="eight column"><a class="slider-img" href="'.$link.'">'.$main_thumb.'</a></div>';
  	    	    $str .= '<div class="eight column"><p class="flex-caption">';
			    $str .= '<span class="sliderCat"><a href="'.$cat_link.'" title="View all posts in '.$cat_name.'">'.$cat_name.'</a></span>';
			   	$str .= '<span class="slidertitle"><a href="'.$link.'">'.$short_title.'</a></span>';
			    $str .= '<span class="slidertext">'. $excerpt . '</span>';
			    $str .= '</p></div>';
  	    		$str .= '</div></li>';

			endwhile;

		endif;

		$str .= '</ul></div></section>';

		wp_reset_postdata();

		return $str;
	}

	$news_slider_category = of_get_option("news_slider_category");
	$news_slider_speed = of_get_option("news_slider_speed");
	$news_slider_transition = of_get_option("news_slider_transition");
	$news_slider_transition = $news_slider_transition == 0 ? "fade" : "slide";

	$news_slider_direction = of_get_option("news_slider_direction");
	$news_slider_direction = $news_slider_direction == 0 ? "horizontal" : "vertical";

	$number_post = of_get_option("news_slider_numberpost");

?>

<div class="row">
	<?php echo vnnews_display_newslider($news_slider_category, $number_post, $news_slider_speed, $news_slider_transition, $news_slider_direction); ?>
</div>